package com.homedo.odin.solution.controller.mng;

import com.homedo.core.common.base.dto.Meta;
import com.homedo.core.common.base.dto.ResultData;
import com.homedo.core.common.exception.BizException;
import com.homedo.core.common.util.NumberHelper;
import com.homedo.odin.solution.contract.mng.SpecialColumnDetailResponseBean;
import com.homedo.odin.solution.contract.mng.request.column.SpecialColumnListRequestBean;
import com.homedo.odin.solution.contract.mng.response.column.SpecialColumnPageResponseBean;
import com.homedo.odin.solution.model.request.mng.column.SpecialColumnRequestBean;
import com.homedo.odin.solution.service.SpecialColumnServiceImpl;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.validation.Valid;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;


/**
 * @author: ZhangLei
 * @date: 2018/4/9
 * @Description:
 */
@Api(description = "栏目", tags = {"Column"},
        produces = MediaType.APPLICATION_JSON_VALUE)
@RestController
@RequestMapping("/mng/special_columns")
public class MngSpecialColumnsController {

    @Autowired
    private SpecialColumnServiceImpl specialColumnService;

    @PostMapping
    @ApiOperation(value = "OMS_新增栏目", httpMethod = "POST")
    public ResultData addColumn(@Valid @RequestBody SpecialColumnRequestBean requestBean, BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            return ResultData.newInstance(Meta.CODE_451, defaultMessage);
        }
        specialColumnService.addColumn(requestBean);
        return ResultData.newInstance(Meta.CODE_200, null);
    }

    @GetMapping
    @ApiOperation(value = "OMS_分页查询栏目列表", response = SpecialColumnPageResponseBean.class, httpMethod = "GET")
    public ResultData querySpecialColumnListByParams(@Valid @ModelAttribute SpecialColumnListRequestBean requestBean,
            BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            return ResultData.newInstance(Meta.CODE_451, defaultMessage);
        }
        SpecialColumnPageResponseBean specialColumnResponseBean = specialColumnService
                .querySpecialColumnListByParams(requestBean);
        return ResultData.newInstance(Meta.CODE_200, specialColumnResponseBean);
    }

    @GetMapping("/{columnId}")
    @ApiOperation(value = "OMS_查看栏目详情", response = SpecialColumnDetailResponseBean.class, httpMethod = "GET")
    public ResultData querySpecialColumnDetail(
            @ApiParam(value = "栏目编号", required = true) @PathVariable("columnId") Long columnId) {
        if (!NumberHelper.validate(columnId)) {
            throw new BizException(Meta.CODE_451.getCode(), "columnId参数非法");
        }
        SpecialColumnDetailResponseBean detailResponseBean = specialColumnService.specialColumnDetail(columnId);
        return ResultData.newInstance(Meta.CODE_200, detailResponseBean);
    }


    @PutMapping
    @ApiOperation(value = "OMS_修改栏目详情", httpMethod = "PUT")
    public ResultData editSpecialColumn(@Valid @RequestBody SpecialColumnRequestBean requestBean,
            BindingResult bindingResult) {
        if (bindingResult.hasErrors()) {
            String defaultMessage = bindingResult.getFieldError().getDefaultMessage();
            return ResultData.newInstance(Meta.CODE_451, defaultMessage);
        }
        specialColumnService.updateSpecialColumn(requestBean);
        return ResultData.newInstance(Meta.CODE_200, null);
    }


    @PutMapping("/{columnId}")
    @ApiOperation(value = "OMS_发布/删除 栏目", httpMethod = "PUT")
    @ApiImplicitParams({
            @ApiImplicitParam(paramType = "path", dataType = "Long", name = "columnId", value = "用戶ID", required = true),
            @ApiImplicitParam(paramType = "path", dataType = "Integer", name = "status", value = "操作状态 20：发布 30：删除", required = true),
            @ApiImplicitParam(paramType = "form", dataType = "Integer", name = "userId", value = "操作人ID", required = true),
            @ApiImplicitParam(paramType = "form", dataType = "String", name = "opretor", value = "操作人", required = true)})
    public ResultData publishSpecialColumn(@PathVariable Long columnId, @RequestParam Integer status,
            @RequestParam Long userId, @RequestParam String opretor) {
        if (!NumberHelper.validate(columnId)) {
            throw new BizException(Meta.CODE_451.getCode(), "columnId参数非法");
        }
        specialColumnService.publishSpecialColumn(columnId, status, userId, opretor);
        return ResultData.newInstance(Meta.CODE_200, null);
    }

}
